/**
 * Created on 2016-1-20
 * filename: SerializeUtil.java
 * Description: 
 * Copyright: Copyright(c)2013
 * Company: Bigdatacluster
 */
package com.codingapi.tx.framework.utils.serializer;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.lorne.core.framework.exception.SerializerException;

/**
 * Title: Class XXXX Description:
 * 
 * 
 * @author yangyulei
 * @version v0.0.1
 */
public class KryoSerializeSerializer implements ISerializer{
	
	/**
	 * 序列化
	 * 
	 * @param object
	 * @return
	 */
	@Override
	public byte[] serialize(Object obj) throws SerializerException {
		Kryo kryo = new Kryo();
		byte[] buffer = new byte[2048];
		Output output = new Output(buffer);
		kryo.writeClassAndObject(output, obj);
		return output.toBytes();
	}

	/**
	 * 反序列化
	 * 
	 * @param bytes
	 * @return
	 */
	@Override
	public <T> T deSerialize(byte[] param) throws SerializerException {
		if(param == null) return null;
		Kryo kryo = new Kryo();
		Input input = new Input(param);
		return (T) kryo.readClassAndObject(input);
	}
}
